package com.fattyCorps.game.controller;


import com.fattyCorps.game.obj.msg.HttpStatus;
import com.fattyCorps.game.obj.msg.SRet;
import com.fattyCorps.game.obj.msg.client.global.CCloseServerMsg;
import com.fattyCorps.game.service.GlobalService;
import com.fattyCorps.game.service.RankService;
import com.fattyCorps.game.cache.PlayerCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/global")
public class GlobalController {

    private final String CLOSE_SERVER_PASSWORD = "HDosdfjoi*((&#$*(_jsldf702314sfd";


    @Autowired
    GlobalService globalService;

    @Autowired
    PlayerCache playerCache;

    @Autowired
    RankService rankService;

    @PostMapping("/closeSever")
    public SRet closeSever(HttpServletRequest request, @RequestBody CCloseServerMsg cCloseServerMsg){
        if (cCloseServerMsg.getPwd().equals(CLOSE_SERVER_PASSWORD)){
            // 设置服务器状态为已经关闭
            globalService.onServerClose();

//            // 将内存数据保存在数据库中
//            playerCache.saveAll();
//            // 保存排行榜数据
//            rankService.saveDb();

            return SRet.success();
        }

        return SRet.error(HttpStatus.CLOSE_SERVER_PWD_ERROR.getCode(), HttpStatus.CLOSE_SERVER_PWD_ERROR.getMsg());
    }
}
